package com.niit.cms.controller;

import com.github.pagehelper.PageInfo;
import com.niit.cms.entity.*;
import com.niit.cms.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: cao_jiaqi
 * @CreateTime： 2022/6/26 11:27
 */
@Controller
public class CourseController {
    @Resource
    private ClassificationService classificationService;
    @Resource
    private CourseService courseService;
    @Resource
    private CatalogService catalogService;
    @Resource
    private MyCourseService myCourseService;
    @Resource
    private KeywordService keywordService;
    /**
     * @Description: 向课程页返回课程信息
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/26 11:33
     * @return: java.lang.String
     **/
    @GetMapping("/course")
    public String course(Model model, Course course,
                         @RequestParam(name = "sort", required = false) String sort,
                         @RequestParam(name = "keyword", required = false) String keyword){
        System.out.println("接收的参数 " + course);
        model.addAttribute("course", course);
        model.addAttribute("sort", sort);
        model.addAttribute("keyword", keyword);
        //查询一级分类
        List<Classification> classificationList = classificationService.queryListByParentId(0);
        //System.out.println("classificationList " + classificationList);
        model.addAttribute("classificationList", classificationList);
        //查询一级分类下的子分类, 默认父分类id为1
        List<Classification> subClassificationList = classificationService.queryListByParentId(1);
        //System.out.println("subClassificationList " + subClassificationList);
        model.addAttribute("subClassificationList", subClassificationList);


        List<Course> courseList;
        //是否要排序
        if(sort != null && sort != ""){
            //是否传入关键词
            if(keyword != null && keyword != ""){
                //如果分类id为空
                if(course.getClassId() == null){
                    courseList = courseService.queryOrderedListByCourseKeyword(course, sort, keyword);
                }
                //如果传回的是父分类id
                else if(course.getClassId() != null && (classificationService.queryById(course.getClassId())).getParentId() == 0){
                    courseList = courseService.queryOrderedListByParentTypeKeyword(course, sort, keyword);
                }
                //如果传回子分类id
                else{
                    courseList = courseService.queryOrderedListByCourseKeyword(course, sort, keyword);
                }
            }else{
                //如果分类id为空
                if(course.getClassId() == null){
                    courseList = courseService.queryOrderedListByCourse(course, sort);
                }
                //如果传回的是父分类id
                else if(course.getClassId() != null && (classificationService.queryById(course.getClassId())).getParentId() == 0){
                    courseList = courseService.queryOrderedListByParentType(course, sort);
                }
                //如果传回子分类id
                else{
                    courseList = courseService.queryOrderedListByCourse(course, sort);
                }
            }
        }else{
            //是否传入关键词
            if(keyword != null && keyword != ""){
                //如果分类id为空
                if(course.getClassId() == null){
                    courseList = courseService.queryListByCourseKeyword(course, keyword);
                }
                //如果传回的是父分类id
                else if(course.getClassId() != null && (classificationService.queryById(course.getClassId())).getParentId() == 0){
                    courseList = courseService.queryListByParentTypeKeyword(course, keyword);
                }
                //如果传回子分类id
                else{
                    courseList = courseService.queryListByCourseKeyword(course, keyword);
                }
            }else{
                //如果分类id为空
                if(course.getClassId() == null){
                    courseList = courseService.queryListByCourse(course);
                }
                //如果传回的是父分类id
                else if(course.getClassId() != null && (classificationService.queryById(course.getClassId())).getParentId() == 0){
                    courseList = courseService.queryListByParentType(course);
                }
                //如果传回子分类id
                else{
                    courseList = courseService.queryListByCourse(course);
                }
            }
        }
        //分页查询课程， 返回pageInfo
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        //System.out.println("pageInfo = " + pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "course";
    }
    /**
     * @Description: 课程详情页
     * @Author: cao_jiaqi
     * @CreateTime: 2022/6/30 16:29
     * @param: courseId
     * @return: java.lang.String
     **/
    @GetMapping("/courseDetail")
    public String courseDetail(Model model, @RequestParam(name = "courseId", required = false) Integer courseId){
        Course course = courseService.queryById(courseId);
        //秒转时分秒
        if(course.getTime() != null) {
            course.setFormatTime(transFom(course.getTime()));
        }
        model.addAttribute("course", course);
        //获取该课程的分类
        Classification classification = classificationService.queryById(course.getClassId());
        model.addAttribute("classification", classification);
        //获取该课程下的目录
        List<Catalog> catalogList = catalogService.queryListByCourseId(courseId);
        model.addAttribute("catalogList", catalogList);
        //课程推荐
        Course c = new Course();
        c.setClassId(course.getClassId());
        List<Course> courseList = courseService.queryOrderedListByCourseLimit(c, "people_num", 4);
        model.addAttribute("courseList", courseList);
        //课程反馈
        MyCourse myCourse = new MyCourse();
        myCourse.setCourseId(course.getId());
        List<MyCourse> myCourseList = myCourseService.queryByMyCourse(myCourse);
        System.out.println("myCourseList " + myCourseList);
        model.addAttribute("myCourseList", myCourseList);
        return "course_detail";
    }
    /**
     * @Description: 通过关键词查询
     * @Author: cao_jiaqi
     * @CreateTime: 2022/7/6 9:38
     * @param: model
     * @param: course
     * @param: keyword
     * @return: java.lang.String
     **/
    @GetMapping("/queryCourseByKeyword")
    public String queryCourseByKeyword(Model model, Course course, @RequestParam("keyword") String keyword){
        //查询一级分类
        List<Classification> classificationList = classificationService.queryListByParentId(0);
        //System.out.println("classificationList " + classificationList);
        model.addAttribute("classificationList", classificationList);
        //查询一级分类下的子分类, 默认父分类id为1
        List<Classification> subClassificationList = classificationService.queryListByParentId(1);
        //System.out.println("subClassificationList " + subClassificationList);
        model.addAttribute("subClassificationList", subClassificationList);

        Keyword kw = keywordService.queryByKw(keyword);
        //如果关键词存在，点击量加1,；否则插入一条关键词
        if(kw != null){
            kw.setCount(kw.getCount() + 1);
            keywordService.update(kw);
        }else{
            Keyword kw2 = new Keyword();
            kw2.setKw(keyword);
            kw2.setCount(1);
            keywordService.insert(kw2);
        }
        //按关键词查询课程
        List<Course> courseList = courseService.queryListByKeyword(course, keyword);
        //分页查询课程， 返回pageInfo
        PageInfo<Course> pageInfo = new PageInfo<>(courseList);
        //System.out.println("pageInfo = " + pageInfo);
        model.addAttribute("keyword", keyword);
        model.addAttribute("pageInfo", pageInfo);
        return "course";
    }

    /**
     *  秒转为时分秒
     * @param time
     * @return
     */
    public static String transFom(Integer time) {
        Integer hh = time / 3600;
        Integer mm = (time % 3600) / 60;
        Integer ss = (time % 3600) % 60;
        return (hh < 10 ? ("0" + hh) : hh) + ":" + (mm < 10 ? ("0" + mm) : mm) + ":" + (ss < 10 ? ("0" + ss) : ss);
    }
}
